﻿package Common {
	
	import flash.display.MovieClip;
    import flash.utils.Timer;
    import flash.events.TimerEvent;
	import flash.display.Stage;
	import flash.events.MouseEvent;
	
	public class Main extends MovieClip {
		
		public const rHIGH = 10;
		public const rLOW = -10;
		
		public const WIDTH = stage.stageWidth;
		public const REMOVE_WIDTH = WIDTH + 100;
		
		public const TIME_ROCK_MOVE = 33;
		
		public const EMITER_X = 0;
		public const EMITER_Y = 100;
		
		public var rockCounter:uint = 0;
		
		public var floorY;
		public const FRICTION = 0.98;
		
		public const GENERATE_EVERY_TICK = 15;
		
		public const TAIL_LENGTH = 15;
		public var tail:Array = new Array();
		public var tailCoord:Array = new Array();
		public var gravity:uint = 1;
		
		public var rocks:Array = new Array();
		
		public var rockCount:uint = 10;
		public var rockYAcc:int = 0;

		public var rock:MovieClip;
		public var timer:Timer = new Timer(TIME_ROCK_MOVE);
		
		public function Main() {
			this.Initialize();
			trace(WIDTH);
		}
		
		public function Initialize()
		{
			this.floorY = this.hFloor.y;
			
			var rock = new rock_1();
			rock.x = 0;
			rock.y = 100;
			rock.rockYAcc = 0;
			rock.rockXSpeed = Math.round(Math.random() * 9) + 1;
			rock.rotSpeed = Math.floor(Math.random()*(1+rHIGH-rLOW))+rLOW;
			rock.tail = new Array();
			rock.tailObj = new Array();
			rock.name = 'rock_' + this.rockCounter;
			
			
			
			for(var i =0; i < TAIL_LENGTH; i++)
			{
				rock.tailObj[i] = new smoke_1();
				rock.tailObj[i].alpha = i/TAIL_LENGTH;
				rock.tailObj[i].scaleX = i/TAIL_LENGTH;
				rock.tailObj[i].scaleY = i/TAIL_LENGTH;
				rock.tailObj[i].name = 'rock_' + this.rockCounter + '_' + i;
				rock.tailObj[i].x = -1000;
				addChild(rock.tailObj[i]);
			}
			
			this.rocks.push(rock);
			
			addChild(rock);
			
			this.rockCounter++;
			
			timer.start();
			timer.addEventListener(TimerEvent.TIMER, this.MoveRock);
		}
		
		public function MoveRock(e:TimerEvent)
		{
	
			for(var i = 0; i < this.rocks.length; i++)
			{
				this.rocks[i].rockYAcc+= this.gravity;
				this.rocks[i].y+= this.rocks[i].rockYAcc;
				this.rocks[i].x+= this.rocks[i].rockXSpeed;
				this.rocks[i].rotation+= this.rocks[i].rotSpeed;
				
				if(this.rocks[i].y > this.floorY)
				{
	
					this.rocks[i].rockYAcc*= - FRICTION;
					this.rocks[i].y = this.floorY;
					
				}
							
				var obj = new Object();
				obj.x = this.rocks[i].x;
				obj.y = this.rocks[i].y;
			
				this.rocks[i].tail.push(obj);

				if(this.rocks[i].tail.length > TAIL_LENGTH)
					this.rocks[i].tail.splice(0,1);

				//if(this.rocks[i].tail.length >= TAIL_LENGTH )
					this.MoveTail(this.rocks[i]);		
					
				if(this.rocks[i].x > REMOVE_WIDTH){
					trace(this.rocks[i].name);
					removeChild(getChildByName(this.rocks[i].name));
					for(var j = 0; j < this.rocks[i].tailObj.length; j++)
						removeChild(getChildByName(this.rocks[i].tailObj[j].name));
					this.rocks.splice(i, 1);	
					
				}
			
			}
			

			
			
			
			this.generateRock(timer.currentCount);
			
		}
		
		public function MoveTail(arr)
		{
			for(var i = 0; i < arr.tail.length; i++)
			{
				arr.tailObj[i].x = arr.tail[i].x;
				arr.tailObj[i].y = arr.tail[i].y;
			}			
		}
		
		public function generateRock(tick)
		{
			if(Math.round(Math.random()) && tick%GENERATE_EVERY_TICK == 0){
				var rock = new rock_1();
				rock.x = EMITER_X;
				rock.y = EMITER_Y;
				rock.rockYAcc = 0;
				rock.rotSpeed = Math.floor(Math.random()*(1+rHIGH-rLOW))+rLOW;
				rock.rockXSpeed = Math.round(Math.random() * 9) + 1;
				rock.tail = new Array();
				rock.tailObj = new Array();
				rock.name = 'rock_' + this.rockCounter;

				for(var i =0; i < TAIL_LENGTH; i++)
				{
					rock.tailObj[i] = new smoke_1();
					rock.tailObj[i].alpha = i/TAIL_LENGTH;
					rock.tailObj[i].scaleX = i/TAIL_LENGTH;
					rock.tailObj[i].scaleY = i/TAIL_LENGTH;
					rock.tailObj[i].name = 'rock_' + this.rockCounter + '_' + i;
					rock.tailObj[i].x = -1000;
					addChild(rock.tailObj[i]);
				}
				
				this.rocks.push(rock);
				
				addChild(rock);
				
				this.rockCounter++;

			}
		}

	}
	
}
